package com.happyass.controller;

import com.happyass.pojo.Staff;
import com.happyass.service.StaffInfoService;
import com.happyass.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    StaffInfoService staffInfoService;

    //权限信息
    @RequestMapping("/list")
    public String staffRoleList(Model model) {
        List<Staff> staffRoleList = staffInfoService.staffRoles();
        model.addAttribute("staffRoleList",staffRoleList);
        return "/Table/roleTable";
    }

    //打开修改权限页面
    @RequestMapping("/edit")
    public String roleEdit(@RequestParam(value = "staffId") Integer staffId, Model model) {
        Staff staff = staffInfoService.selectStaffRolesByStaffId(staffId);
        model.addAttribute("staff",staff);
        return "/role/roleEdit";
    }

    @RequestMapping("/submitEdit")
    @ResponseBody
    public HashMap<String, Object> submitRoleEdit(Integer staffId, Integer roleId) {
        boolean staff = roleService.updateRoleByStaffId(staffId,roleId);
        HashMap<String, Object> map = new HashMap<>();
        if (staff){
            map.put("info","修改成功");
            return map;
        }
        map.put("info","修改失败");
        return map;
    }


}
